#include <stdafx.h>

#include <iostream>
#include <PCASolver.h>
#include <CommandLineArgsParser.h>
#include <DataLoader.h>

int main(int argc, char ** argv) {
    CommandLineArgsParser cmd(argc, argv);
    if (cmd.isUsageMessage()) {
        std::cout << cmd.getUsageMessage() << std::endl;
        return 0;
    }
    if (cmd.isErrorMessage()) {
        std::cerr << cmd.getErrorMessage() << std::endl;
        std::cout << cmd.getUsageMessage() << std::endl;
        return 1;
    }

    DataLoader loader;
    Eigen::MatrixXd *inputMatrix = loader.loadMatrix(cmd.getInputPath());
    Eigen::MatrixXd *inputTransformMatrix = loader.loadMatrix(cmd.getInputTranformPath());

    PCASolver PCASolver(inputMatrix, inputTransformMatrix);

    if (cmd.getOutputTranformPath()) {
        loader.saveMatrix(PCASolver.getPCMatrix(), cmd.getOutputTranformPath());
    }
    if (cmd.getOutputPath() != NULL) {
        Eigen::MatrixXd *reducedMatrix = PCASolver.performDimensionReduction(inputMatrix, cmd.getDimensions());
        loader.saveMatrix(reducedMatrix, cmd.getOutputPath());
        delete reducedMatrix;
    }

    delete inputMatrix;
    if (inputTransformMatrix != NULL) {
        delete inputTransformMatrix;
    }
}
